/**
 * 工程名：COMMON
 * 包名：com.mt.common.safe
 * 文件名：Role.java
 */
package com.mt.system.domain;

import java.io.Serializable;
import java.util.Collection;
import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;

import com.mt.component.domain.Common;
import com.mt.utils.StringUtils;



/**
 * 类名称：Role <br>
 * 类描述：系统角色 <br>
 * 创建人： 马腾 <br>
 * 创建时间：2012-10-18 下午11:31:46 <br>
 */
@Entity
@Table(name="CFG_SYS_ROLE")
public class Role extends Common implements Serializable {
	
	private static final long serialVersionUID = 1559045986305123229L;
	
	private String name;//角色名称
	private String desc;//角色描述
	private Date operateTime;
	private Collection<Permission> permissions;//角色对应的权限
	private boolean selected = false;
	
	@Column(name = "ROLE_NAME",length = 32 , nullable=false , unique=true)
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	
	@Column(name = "ROLE_DESC",length = 128)
	public String getDesc() {
		return desc;
	}
	public void setDesc(String desc) {
		this.desc = desc;
	}
	
	@Temporal(TemporalType.TIMESTAMP)
	@Column(name="OPERATE_TIME")
	public Date getOperateTime() {
		return operateTime;
	}
	public void setOperateTime(Date operateTime) {
		this.operateTime = operateTime;
	}
	
	@ManyToMany(fetch=FetchType.LAZY)
	@JoinTable(name = "CFG_SYS_ROLE_PMS",
		joinColumns = {@JoinColumn(name = "ROLE_ID", nullable = false, updatable = false)}, 
		inverseJoinColumns = {@JoinColumn(name = "PMS_ID", nullable = false, updatable = false)}
	)
	public Collection<Permission> getPermissions() {
		return permissions;
	}
	public void setPermissions(Collection<Permission> permissions) {
		this.permissions = permissions;
	}
	
	@Transient
	public boolean isSelected() {
		return selected;
	}
	public void setSelected(boolean selected) {
		this.selected = selected;
	}
	
	@Override
	public boolean equals(Object obj) {
		if(obj instanceof Role){
			Role role = (Role)obj;
			if(StringUtils.isEmpty(role.getId())) return false;
			return role.getId().equals(getId());
		}
		return false;
	}
	
	@Override
	public int hashCode() {
		return getId()==null ? 0 : getId().hashCode();
	}
}
